package edu.unl.bsm.CoolingLoad.ARXModel;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

import jxl.read.biff.BiffException;


import edu.unl.bsm.CoolingLoad.Data.WeatherDataWeekDays;
import edu.unl.bsm.CoolingLoad.Util.LastNHour;



public class ARXSecondWDProvider extends WeatherDataWeekDays implements IARXModel{
	
	private double[][] ARXmodel_X;
	private double[] ARXmodel_Y;
	

	





	public ARXSecondWDProvider() throws ParseException, BiffException, IOException{
		super();
  
	}
	
	public void setHistdata() throws ParseException{
		super.readHistWeatherData();
		int datasize = super.getHistWeatherData().getDataSize();
		this.ARXmodel_X = new double[datasize][17];
		this.ARXmodel_Y = new double[datasize];   
	}
	
	
	public void setForecastData() throws ParseException{
		super.readForecastWeatherData();
		int datasize = super.getForecastWeatherData().getDataSize();
		this.ARXmodel_X = new double[datasize][17];
		this.ARXmodel_Y = new double[datasize];
	}
	
	
	
	/**
	 * @return the aRXmodel
	 */
	public double[][] getARXmodel_X() {
		
		int counter = 0;
		for(int i = 0; i < ARXmodel_X.length; i++){
			if(ARXmodel_X[i][0] == 0){
				counter++;
			}
		}
		
		double[][] result = new double[ARXmodel_X.length-counter][17];
		
		for(int i = 0, j = 0; i < ARXmodel_X.length && j < result.length; i++){
			if(ARXmodel_X[i][0] != 0){
				result[j] = ARXmodel_X[i];
				j++;
			}
		}
		
		return result;
		
		
	}
	
	/**
	 * @return the aRXmodel_Y
	 */
	public double[] getARXmodel_Y() {
		int counter = 0;
		for(int i = 0; i < ARXmodel_Y.length; i++){
			if(ARXmodel_Y[i] == 0){
				counter++;
			}
		}
		
		double[] result = new double[ARXmodel_X.length-counter];
		
		for(int i = 0, j = 0; i < ARXmodel_Y.length && j < result.length; i++){
			if(ARXmodel_Y[i] != 0){
				result[j] = ARXmodel_Y[i];
				j++;
			}
		}
		
		return result;
	}

	/**
	 * Set up ARXmodel for Historical data
	 * @param aRXmodel the aRXmodel to set
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws BiffException 
	 */
	public void setARXHistmodel(int hour) throws ParseException, BiffException, IOException {
		
		Map<Date, String> dateMap = super.getHistWeatherData().getLocalTime();
		List<Date> datelist = new ArrayList<Date>(dateMap.keySet());
		Collections.sort(datelist);
		
		
	
			for(int i = 168; i < datelist.size(); i++){
//				
				Date date = datelist.get(i);
				DateTime dt = new DateTime(date);
				if(dt.getDayOfWeek() >=1 && dt.getDayOfWeek() <= 5){
					
					if(dt.getHourOfDay() == hour){
						//Constant
						ARXmodel_X[i][0] = 1d;
							
							//T_Max_t
						ARXmodel_X[i][1] = super.getHistWeatherData().getT_MAX_t().get(date);
							
							//T_Min_t
						ARXmodel_X[i][2] = super.getHistWeatherData().getT_MIN_t().get(date);
							
							//T_t
						ARXmodel_X[i][3] = super.getHistWeatherData().getT_t().get(date);
							
							//T_t * T_t
						ARXmodel_X[i][4] = ARXmodel_X[i][3] * ARXmodel_X[i][3];
							
							//RH_t
						ARXmodel_X[i][5] = super.getHistWeatherData().getRH_t().get(date);
							
							//RH_t * T_t
						ARXmodel_X[i][6] = ARXmodel_X[i][5] * ARXmodel_X[i][3];
							
						
						// if monday, t-24 will be turned into last friday
						if(dt.getDayOfWeek() == 1){
									//Cool load t-24
								ARXmodel_X[i][7] = LastNHour.getLastNHour(date, 72, super.getHistWeatherData()).getCoolLoadt();
									
									//T_t-24
								ARXmodel_X[i][8] = LastNHour.getLastNHour(date, 72, super.getHistWeatherData()).getTt();
									
									//T_t-24 * T_t-24
								ARXmodel_X[i][9] = ARXmodel_X[i][8]*ARXmodel_X[i][8];
									
									//RH_t-24
								ARXmodel_X[i][10] = LastNHour.getLastNHour(date, 72, super.getHistWeatherData()).getRHt();
									
									//T_t-24 * RH_t-24
								ARXmodel_X[i][11] = ARXmodel_X[i][8]*ARXmodel_X[i][10];
							
						}else{
							
							//Cool load t-24
							ARXmodel_X[i][7] = LastNHour.getLastNHour(date, 24, super.getHistWeatherData()).getCoolLoadt();
								
								//T_t-24
							ARXmodel_X[i][8] = LastNHour.getLastNHour(date, 24, super.getHistWeatherData()).getTt();
								
								//T_t-24 * T_t-24
							ARXmodel_X[i][9] = ARXmodel_X[i][8]*ARXmodel_X[i][8];
								
								//RH_t-24
							ARXmodel_X[i][10] = LastNHour.getLastNHour(date, 24, super.getHistWeatherData()).getRHt();
								
								//T_t-24 * RH_t-24
							ARXmodel_X[i][11] = ARXmodel_X[i][8]*ARXmodel_X[i][10];
							
						}
							
							//Cool load t-168
						ARXmodel_X[i][12] = LastNHour.getLastNHour(date, 168, super.getHistWeatherData()).getCoolLoadt();
							
							//T_t-168
						ARXmodel_X[i][13] = LastNHour.getLastNHour(date, 168, super.getHistWeatherData()).getTt();
							
							//T_t-168 * T_t-168
						ARXmodel_X[i][14] = ARXmodel_X[i][13]*ARXmodel_X[i][13];
							
							//RH_t-168
						ARXmodel_X[i][15] = LastNHour.getLastNHour(date, 168, super.getHistWeatherData()).getRHt();
							
							//T_t-168 * RH_t-168
						ARXmodel_X[i][16] = ARXmodel_X[i][13]*ARXmodel_X[i][15];
							
						
						
						//Cool load
						ARXmodel_Y[i] = super.getHistWeatherData().getCoolLoad_t().get(date);
					}
				}
				
				
		}
	}
	
	
	/**
	 * Set up ARXModel for Future data
	 * @throws ParseException
	 * @throws BiffException
	 * @throws IOException
	 */
	public void setARXForcastmodel() throws ParseException, BiffException, IOException {
			
			Map<Date, String> dateMap = super.getForecastWeatherData().getLocalTime();
			
			
			
			List<Date> datelist = new ArrayList<Date>(dateMap.keySet());
			
			Collections.sort(datelist);
			
			
		
				for(int i = 168; i < datelist.size(); i++){
	//				
					Date date = datelist.get(i);
					DateTime dt = new DateTime(date);
					if(dt.getDayOfWeek() >=1 && dt.getDayOfWeek() <= 5){
							//Constant
							ARXmodel_X[i][0] = 1d;
								
								//T_Max_t
							ARXmodel_X[i][1] = super.getForecastWeatherData().getT_MAX_t().get(date);
								
								//T_Min_t
							ARXmodel_X[i][2] = super.getForecastWeatherData().getT_MIN_t().get(date);
								
								//T_t
							ARXmodel_X[i][3] = super.getForecastWeatherData().getT_t().get(date);
								
								//T_t * T_t
							ARXmodel_X[i][4] = ARXmodel_X[i][3] * ARXmodel_X[i][3];
								
								//RH_t
							ARXmodel_X[i][5] = super.getForecastWeatherData().getRH_t().get(date);
								
								//RH_t * T_t
							ARXmodel_X[i][6] = ARXmodel_X[i][5] * ARXmodel_X[i][3];
								
							
							// if monday, t-24 will be turned into last friday
							if(dt.getDayOfWeek() == 1){
										//Cool load t-24
									ARXmodel_X[i][7] = LastNHour.getLastNHour(date, 72, super.getForecastWeatherData()).getCoolLoadt();
										
										//T_t-24
									ARXmodel_X[i][8] = LastNHour.getLastNHour(date, 72, super.getForecastWeatherData()).getTt();
										
										//T_t-24 * T_t-24
									ARXmodel_X[i][9] = ARXmodel_X[i][8]*ARXmodel_X[i][8];
										
										//RH_t-24
									ARXmodel_X[i][10] = LastNHour.getLastNHour(date, 72, super.getForecastWeatherData()).getRHt();
										
										//T_t-24 * RH_t-24
									ARXmodel_X[i][11] = ARXmodel_X[i][8]*ARXmodel_X[i][10];
								
							}else{
								
								//Cool load t-24
								ARXmodel_X[i][7] = LastNHour.getLastNHour(date, 24, super.getForecastWeatherData()).getCoolLoadt();
									
									//T_t-24
								ARXmodel_X[i][8] = LastNHour.getLastNHour(date, 24, super.getForecastWeatherData()).getTt();
									
									//T_t-24 * T_t-24
								ARXmodel_X[i][9] = ARXmodel_X[i][8]*ARXmodel_X[i][8];
									
									//RH_t-24
								ARXmodel_X[i][10] = LastNHour.getLastNHour(date, 24, super.getForecastWeatherData()).getRHt();
									
									//T_t-24 * RH_t-24
								ARXmodel_X[i][11] = ARXmodel_X[i][8]*ARXmodel_X[i][10];
								
							}
								
								//Cool load t-168
							ARXmodel_X[i][12] = LastNHour.getLastNHour(date, 168, super.getForecastWeatherData()).getCoolLoadt();
								
								//T_t-168
							ARXmodel_X[i][13] = LastNHour.getLastNHour(date, 168, super.getForecastWeatherData()).getTt();
								
								//T_t-168 * T_t-168
							ARXmodel_X[i][14] = ARXmodel_X[i][13]*ARXmodel_X[i][13];
								
								//RH_t-168
							ARXmodel_X[i][15] = LastNHour.getLastNHour(date, 168, super.getForecastWeatherData()).getRHt();
								
								//T_t-168 * RH_t-168
							ARXmodel_X[i][16] = ARXmodel_X[i][13]*ARXmodel_X[i][15];
								
							
							
							//Cool load
							ARXmodel_Y[i] = super.getForecastWeatherData().getCoolLoad_t().get(date);
					}
					
					
			}
		}

}
